<template>
  <div class="app-container">
    <div class="search-div">
      <el-form label-width="70px" size="small" :inline="true">
        <el-row>
          <el-form-item label="用户名">
            <el-input style="width: 100%" v-model.trim="searchObj.username" placeholder="用户名"></el-input>
          </el-form-item>
          <el-form-item label="姓名">
            <el-input style="width: 100%" v-model.trim="searchObj.name" placeholder="姓名"></el-input>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input style="width: 100%" v-model.trim="searchObj.phone" placeholder="手机号"></el-input>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="createTimes"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              value-format="yyyy-MM-dd HH:mm:ss"
              style="margin-right: 10px;width: 100%;"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" :loading="loading" @click="fetchData()">搜索
            </el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetData">重置</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="spacer"/>
    <!-- 工具条 -->
    <div class="tools-div">
      <el-button type="primary" icon="el-icon-plus" size="mini" @click="add">添 加</el-button>
      <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeBatch">批量删除</el-button>
    </div>
    <!-- 列表 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      border
      style="width: 100%;margin-top: 10px;"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection"/>
      <el-table-column
        label="序号"
        width="70"
        align="center"
      >
        <template slot-scope="scope">
          {{ (pageNo - 1) * pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="username" label="用户名"/>
      <el-table-column prop="name" label="姓名"/>
      <el-table-column prop="phone" label="手机"/>
      <el-table-column prop="postName" label="岗位"/>
      <el-table-column prop="deptName" label="部门"/>
      <el-table-column prop="ofRole" label="所属角色"/>
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status === 1"
            @change="switchStatus(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="160"/>

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" title="修改" @click="edit(scope.row.id)"/>
          <el-button type="danger" icon="el-icon-delete" size="mini" title="删除"
                     @click="removeDataById(scope.row.id)"/>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      :current-page="pageNo"
      :total="total"
      :page-size="pageSize"
      style="padding: 30px 0; text-align: center;"
      layout="prev, pager, next"
      @current-change="fetchData"
    />
    <!-- 弹出框-->
    <el-dialog title="添加/修改" :visible.sync="dialogVisible" width="40%">
      <el-form ref="dataForm" :model="sysUser" label-width="100px" size="small" style="padding-right: 40px;">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="sysUser.username"/>
        </el-form-item>
        <el-form-item v-if="!sysUser.id" label="密码" prop="password">
          <el-input v-model="sysUser.password" type="password"/>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="sysUser.name"/>
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model="sysUser.phone"/>
        </el-form-item>
        <el-form-item label="部门">
          <el-cascader
            style="width: 100%"
            :props="treeProps"
            v-model="sysUser.deptId"
            :options="deptList"
            @change="deptIdClick"
            clearable
            placeholder="请选择部门"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="岗位">
          <el-select
            v-model="sysUser.postId"
            placeholder="请选择岗位"
            style="width:100%;"
          >
            <el-option
              style="font-weight: bold"
              v-for="item in postList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属角色">
          <el-select
            v-model="sysUser.ofRole"
            placeholder="请选择所属角色"
            style="width:100%;"
            multiple
          >
            <el-option
              style="font-weight: bold"
              v-for="item in roleList"
              :label="item.roleName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small" icon="el-icon-refresh-right">取 消</el-button>
        <el-button type="primary" icon="el-icon-check" @click="saveOrUpdate()" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import roleApi from '@/api/system/sysRole'
import api from '@/api/system/sysUser'
import postApi from '@/api/system/sysPost'
import deptApi from '@/api/system/sysDept'
import sysUser from "@/api/system/sysUser";

export default {
  name: 'Index',
  data() {
    return {
      multipleSelection: [],  // 批量删除选择的数据
      treeProps: {
        'label': 'name',
        'value': 'id',
        'children': 'child',
        'checkStrictly': true
      },
      deptList: [],
      roleList: [],
      postList: [], // 岗位
      roleLoading: false,
      loading: false,
      listLoading: true, // 数据是否正在加载
      list: [], //
      total: 0,   //数据库中的总记录数
      pageNo: 1, // 默认页码
      pageSize: 10,  // 每页记录数
      searchObj: {},  //搜索对象
      createTimes: [],
      dialogVisible: false,
      sysUser: {},
      saveBtnDisabled: false
    }
  },
  created() {
    this.fetchData()
    this.remotePost()
    this.remoteRole()
    this.remoteDept()
  },
  methods: {
    fetchData(pageNo = 1) {
      this.loading = true
      this.listLoading = true
      this.pageNo = pageNo
      if (this.createTimes && this.createTimes.length === 2) {
        this.searchObj.createTimeBegin = this.createTimes[0]
        this.searchObj.createTimeEnd = this.createTimes[1]
      }
      api.getPageList(this.pageNo, this.pageSize, this.searchObj).then(res => {
        if (res.code === 200) {
          this.list = res.data.records
          this.total = res.data.total
          this.listLoading = false
          this.loading = false
        }
      })
    },
    removeDataById(id) {
      this.$confirm('是否删除该数据', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.deleteById(id).then(res => {
          this.$message.success('删除成功')
          this.fetchData()
        })
      })
    },
    add() {
      this.dialogVisible = true
      this.sysUser = {}
    },
    edit(id) {
      this.dialogVisible = true
      api.getById(id).then(res => this.sysUser = res.data)
    },
    switchStatus(row) {
      row.status = row.status === 1 ? 0 : 1
      api.updateStatus(row.id,row.status).then(res =>{})
    },
    resetData() {
      this.createTimes = []
      this.searchObj = {}
      this.fetchData(1)
    },
    saveOrUpdate() {
      if (!this.sysUser.id) {
        this.save()
      } else {
        this.update()
      }

    },
    update() {
      console.log(this.sysUser.ofRole);
      api.update(this.sysUser).then(res => {
        this.$message.success("保存成功")
        this.dialogVisible = false
        this.fetchData()
      })
    },
    save() {
      if (!this.sysUser.deptId) {
        this.$message.error('请选择部门')
        return
      }
      if (!this.sysUser.ofRole) {
        this.$message.error('请选择所属角色')
        return;
      }
      api.save(this.sysUser).then(res => {
        this.$message.success('保存成功')
        this.dialogVisible = false
        this.fetchData()
      })

    },
    removeBatch() {
      let ids = this.multipleSelection.map(s => s.id);
      this.$confirm('是否删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        api.deleteBatchById(ids).then(res => {
          this.$message.success('删除成功')
          this.fetchData()
        })
      })


    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
    },
    remoteRole() {
      roleApi.getAllRole().then(res => {
        this.roleList = res.data
      })
    },
    remotePost() {
      postApi.getAllPost().then(res => {
        if (res.code === 200) {
          this.postList = res.data
        }
      })
    },
    remoteDept() {
      deptApi.getAllDept().then(res => {
        this.deptList = res.data
      })
    },
    deptIdClick(data) {
    }
  }
}
</script>

<style scoped>
.spacer {
  display: inline-block;
  width: 50px;
}

.el-button--primary {
  background-image: linear-gradient(to right, #00C9FF, #92FE9D);
}

.el-tree {
  background: linear-gradient(to bottom, #FFFFFF, #CCCCCC);
}

.el-checkbox {
  width: 15px;
  height: 15px;
}
</style>
